<!DOCTYPE html>
<html lang="zh-cn">
<head>
<title>月光边境</title>
<meta name="keywords" content="关键词" />
<meta name="description" content="描述" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<th:block th:include="common/style::style" />
</head>
<body>
	<div class="container">
		<div class="panel">
			<div class="panel-head">
				<form id="searchForm" th:action="@{'/common/'+${table}+'/list'}">
					<input type="button" class="button border-green" value="新增" onclick="toAdd()" />
					<select name="type" class="input input-auto border-main">
						<option value="1" th:selected="${type=='1'}">模糊查询</option>
						<option value="2" th:selected="${type=='2'}">精确查询</option>
					</select>
					<select name="column" class="input input-auto border-main">
						<option value="">列名</option>
					</select>
					<input name="keyword" type="text" th:value="${keyword}" style="margin-left: -5px;" class="input input-auto border-main" placeholder="输入关键字"/> 
					<input type="submit" value="搜索" class="button bg-main" style="border-left: 0 none;margin-left: -10px;border-top-left-radius:0;border-bottom-left-radius:0" />
				</form>
			</div>
			<div id="list">
				<table class="table">
					<thead>
						<tr>
							<th>数据</th>
							<th width="120">操作</th>
						</tr>
					</thead>
					<tr th:each="data:${datas}">
						<td><pre th:text="${data.json}"></pre></td>
						<td>
						<a href="javascript:;" class="button border-blue button-little" th:onclick="'toEdit(\''+${data.data.id}+'\')'">编辑</a>
						<a href="javascript:;" class="button border-red button-little" th:onclick="'del(\''+${data.data.id}+'\')'">删除</a>
						</td>
					</tr>
<!-- 					<tbody> -->
<!-- 						<tr> -->
<!-- 							<td><pre></pre></td> -->
<!-- 							<td> -->
<!-- 								<a href="javascript:;" class="button border-blue button-little">编辑</a> -->
<!-- 								<a href="javascript:;" class="button border-red button-little">删除</a> -->
<!-- 							</td> -->
<!-- 						</tr> -->
<!-- 					</tbody> -->
				</table>
			</div>
			<div class="panel-foot text-center" id="pagination">
				<form id="pageForm" th:action="@{'/common/'+${table}+'/list'}">
					<th:block th:include="common/pagination::pagination"/>
					<input type="hidden" name="type" th:value="${type}"/>
					<input type="hidden" name="column" th:value="${column}"/>
					<input type="hidden" name="keyword" th:value="${keyword}"/>
				</form>
			</div>
		</div>
	</div>
</body>
<script type="text/javascript">
	$(function(){
		$.get(ctx + 'common/tables/[(${table})].json').done(function(data){
			$.each(data.data.normalColumns,function(i,n){
				$('select[name="column"]').append('<option value="'+n.column+'">'+n.column+'</option>');
			});
			$('select[name="column"]').val('[(${column})]');
		});
		// 列表
		//listData();
	});
	function listData(){
		$.get(ctx + 'common/[(${table})].json?'+zutil.formData($('#pageForm'))).done(function(data){
			var $tr = $('#list tbody tr').first().remove();
			$.each(data.rows,function(i,n){
				$tr = $tr.clone();
				$tr.find('pre').html(JSON.stringify(n, null, 2));
				$tr.find('a').first().attr('onclick','toEdit(\''+n['id']+'\')')
				$tr.find('a').last().attr('onclick','del(\''+n['id']+'\')')
				$('#list tbody').append($tr);
			});
		});
	}
	function toAdd(){
		$.get(ctx + 'common/[(${table})]/form').done(function(data){
			layer.open({
				title:'新增',
				type:1,
				shadeClose: true,
				area:['600px','400px'],
				content:data
			});
			$('#dataForm button').click(function(){
				var $form = $('#dataForm');
				$form.attr('action',ctx + 'common/[(${table})].json');
				$form.ajaxSubmit(function(){
					$form.find(':input[data-type]').each(function(){
						if($.trim($(this).val())==''){
							$(this).attr('name','');
						}
					});
					zutil.restful($form,'POST',function(){
						location.href = location.href;
					});
				});
			});
		});
	}
	function toEdit(id){
		$.get(ctx + 'common/[(${table})]/form').done(function(form){
			$.get(ctx + 'common/[(${table})]/'+id+'.json').done(function(data){
				layer.open({
					title:'修改',
					type:1,
					shadeClose: true,
					area:['600px','400px'],
					content:form
				});
				// 回填
				$('#dataForm :input').each(function(){
					val = data.data[$(this).attr('name')];
					// long时间戳格式化
					if($(this).attr('data-type')=='TIMESTAMP' && !isNaN(val)){
						val = new Date(parseInt(val)).Format('yyyy-MM-dd hh:mm:ss');
					}
					$(this).val(val);
				});
				$('#dataForm button').click(function(){
					var $form = $('#dataForm');
					$form.attr('action',ctx + 'common/[(${table})]/'+id+'.json');
					$form.ajaxSubmit(function(){
						$form.find(':input[data-type]').each(function(){
							if($.trim($(this).val())==''){
								$(this).attr('name','');
							}
						});
						zutil.restful($form,'PUT',function(){
							location.href = location.href;
						});
					});
				});
			});
		});
	}
	function del(id){
		layer.confirm('确定删除吗？', {
			shadeClose : true,
			btn : [ '确定','取消' ]
		}, function() {
			var url = ctx + 'common/[(${table})]/'+id+'.json';
			$.ajax({
				url : url,
				type : 'DELETE',
				dataType : 'json',
				success : function(data){
					if(data.retCode==1){
						layer.msg('删除成功');
						location.href = location.href;
					}else{
						layer.msg(data.retMsg);
					}
				}
			});
		}, function() {
			layer.close(layer.index);
		});
	}
</script>
</html>